package t2.client.events;

import t2.client.MainTabPanel;
import t2.client.rpc.NDcmd;
import t2.client.rpc.NDcmdAsync;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class NDcmdEvent extends GwtEvent<NDcmdEventHandler> {

    private static final int DEV_TYPE_COORDINATOR = 0;
    private static final int DEV_TYPE_END_DEVICE = 1;
    private static final int DEV_TYPE_ROUTER = 2;
    private static final int NAME = 0;
    private static final int ADDR = 1;


    public static Type<NDcmdEventHandler> TYPE = new Type<NDcmdEventHandler>();
    final NDcmdAsync ndCmdService = (NDcmdAsync) GWT.create(NDcmd.class);

    private String nodeName;
    int retval = 0;
    private int listIndex;
    private int deviceType;
    private boolean response;
    private int value;


    public NDcmdEvent() {}

    public NDcmdEvent(int deviceType, int listIndex) {
        setListIndex(listIndex);
        setDeviceType(deviceType);
    }


    public NDcmdEvent(boolean response, int deviceType, int value) {
        setResponse(response);
        setDeviceType(deviceType);
        setValue(value);
    }

    public NDcmdEvent(boolean response, int deviceType, String string) {
        setResponse(response);
        setDeviceType(deviceType);
        setNodeName(string);
    }


    @Override
    public com.google.gwt.event.shared.GwtEvent.Type<NDcmdEventHandler> getAssociatedType() {
        return TYPE;
    }

    @Override
    protected void dispatch(NDcmdEventHandler handler) {
        handler.processNDcmdEvent(this);
    }

    public int getNumEndDevs() {
        ndCmdService.getNumEndDevs(new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {}

            @Override
            public void onSuccess(Integer result) {
                MainTabPanel.EVENT_BUS.fireEvent(new NDcmdEvent(true, DEV_TYPE_END_DEVICE, result));
            }

        });
        /* Zero returned for success.  Don't do anything with the value returned here; the value
         * you want is returned in the onSuccess method above. */
        return 0;
    }

    public void rpcQueryCoordName() {
        ndCmdService.rpcQueryCoordName(new AsyncCallback<String>() {

            @Override
            public void onFailure(Throwable caught) {
                setNodeName("failure to get node name.");
            }

            @Override
            public void onSuccess(String result) {
                setNodeName(result);
                MainTabPanel.EVENT_BUS.fireEvent(new NDcmdEvent(true, DEV_TYPE_COORDINATOR, result));
            }
        });
    }

    public String getNodeName(int deviceType, int listIndex) {
        switch (deviceType) {
            case DEV_TYPE_COORDINATOR:
                break;

            case DEV_TYPE_END_DEVICE:
                ndCmdService.getNodeName(DEV_TYPE_END_DEVICE, listIndex, new AsyncCallback<String>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        setNodeName("failure to get end device node name.");
                    }

                    @Override
                    public void onSuccess(String result) {
                        Window.alert("End device is named " + result);
                        setNodeName(result);
                    }
                });
                break;

            case DEV_TYPE_ROUTER:
                ndCmdService.getNodeName(DEV_TYPE_ROUTER, listIndex, new AsyncCallback<String>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onSuccess(String result) {
                        Window.alert("Router is named " + result);
                    }

                });
                break;

            default:
                break;
        };

        return nodeName;
    }


    public int getListIndex() {
        return listIndex;
    }

    public void setListIndex(int listIndex) {
        this.listIndex = listIndex;
    }

    public int getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(int deviceType) {
        this.deviceType = deviceType;
    }

    public boolean isResponse() {
        return response;
    }

    public void setResponse(boolean response) {
        this.response = response;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }
}
